$(document).ready(function() {
	
	editor.setValue('');
	editor.focus();
	$('#status').text(NAO_MODIFICADO);
	
	$(document.documentElement).keypress(function(event) {
			if(event.ctrlKey) {
				if(event.which == 99) {
					$('#copiar').click();
				}else if(event.which == 120) {
					$('#recortar').click();
				} else if(event.which == 118) {
					$('#colar').click();
				} else if(event.which == 97) {
					$('#abrir').click();
				} else if(event.which == 115) {
					$('#salvar').click();
				} else if(event.which == 110) {
					$('#novo').click();
				}
			} else {
				if(event.keyCode == 120) {
						$('#gera_codigo').click();
				} else if(event.keyCode == 119) {
						$('#compilar').click();
				} else if(event.keyCode == 112) {
						$('#equipe').click();
				}
			}
	});
	
	text = '';

	$('#equipe').click(function() {
		$("#dialog_equipe").dialog({
			title : "Equipe",
			modal : true
		});
	});

	$('#novo').click(function() {
            editor.setValue('');
			$('#status').text(NAO_MODIFICADO);

	});
	
	$('#salvar').click(function() {
		$('#status').text(NAO_MODIFICADO);
});

	$('#bt_abrir').click(btAbrirClick);

  $('#copiar').click(function() {
            text = editor.getSelection();
	});

  $('#recortar').click(function() {
            text = editor.getSelection();
            editor.replaceSelection('');
	});

 	$('#colar').click(function() {
            cursor = editor.getCursor();
						conteudo_old = editor.getLine(cursor.line);
						novo_conteudo = conteudo_old.substring(0, cursor.ch) + text + conteudo_old.substring(cursor.ch);
						editor.setLine(cursor.line, novo_conteudo);
	});

	$('#abrir').click(function() {
		$("#file_chooser").dialog({
			width : 400,
			height : 120,
			title : "Abrir",
			modal : true
		});
	});

	$('#compilar').click(btnCompilarClick);	

	$( "#novo" ).button({
		text: true,
		label: "Novo[ctrl-n]",
		icons: {
			primary: "ui-icon-document"
		}
	});
	$( "#abrir" ).button({
		text: true,
		label: "Abrir[ctrl-a]",
		icons: {
			primary: "ui-icon-folder-open"
		}
	});
	$( "#salvar" ).button({
		text: true,
		label: "Salvar[ctrl-s]",
		icons: {
			primary: "ui-icon-disk"
		}
	});
	$( "#copiar" ).button({
		text: true,
		label: "Copiar[ctrl-c]", 
		icons: {
			primary: "ui-icon-copy"
		}
	});
	$( "#colar" ).button({
		text: true,
		label: "Colar[ctrl-v]",
		icons: {
			primary: "ui-icon-document-b"
		}
	});
	$( "#recortar" ).button({
		text: true,
		label: "Recortar[ctrl-x]",
		icons: {
			primary: "ui-icon-scissors"
		}
        });	
        $( "#compilar" ).button({
            text: true,
            label: "Compilar[F8]",
			icons: {
				primary: "ui-icon-play"
			}
        });
        $( "#gera_codigo" ).button({
            text: true,
            label: "Gerar código[F9]",
			icons: {
				primary: "ui-icon-extlink"
			}
        });
        $( "#equipe" ).button({
            text: true,
            label: "Equipe[F1]",
			icons: {
				primary: "ui-icon-person"
			}
	});
});

function showLoading(message) {
	$('#loading_text').text(message);
	$("#loading").dialog({
		modal : true
	});
}

function hideLoading() {
	$("#loading").dialog('close');
}

function btnCompilarClick() {
	showLoading("Aguarde, compilando arquivo!");
	$.ajax({
		url : "/ballyzacompiler",
		type : 'POST',
		data : "value="+replaceAll(editor.getValue(), "+", "2mais2"),
		
		success : function(data) {
            var result = "";
            if(data.success) {
                result += "Token\tClasse\t\tLinha\n";
                $.each(data.lexicalResult, function(i, line) {
                    result += line.token + "\t" + line.klass + "\t" + line.line + "\n";
                });
                result += "\nCompilado com sucesso!";
            } else {
                result += data.errorMessage;
            }

            $("#ta_console").text(result);

			hideLoading();
		}
	});
};

function btAbrirClick() {
		$.ajax({
				url : '/openfile',
				data : 'file=lalala',
				type : 'POST',
				success : function(data) {
					editor.setValue(data);
				}
		});
};

function showAlert(message) {
	alert(message);
};

function replaceAll(string, token, newtoken) {
	while (string.indexOf(token) != -1) {
 		string = string.replace(token, newtoken);
	}
	return string;
}